package org.tigr.htc.server;

/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

import java.io.IOException;
import java.util.Vector;

import org.apache.log4j.Logger;
import org.apache.xmlrpc.XmlRpcClient;
import org.apache.xmlrpc.XmlRpcClientLite;
import org.apache.xmlrpc.XmlRpcException;
import org.tigr.antware.shared.command.events.CommandRuntimeStatusLI;
import org.tigr.antware.shared.command.events.CommandStatusLI;
import org.tigr.antware.shared.command.events.FailureEvent;
import org.tigr.antware.shared.command.events.FinishEvent;
import org.tigr.antware.shared.command.events.InterruptEvent;
import org.tigr.antware.shared.command.events.ResumeEvent;
import org.tigr.antware.shared.command.events.StartEvent;
import org.tigr.antware.shared.command.events.SubmitEvent;
import org.tigr.antware.shared.command.events.SuspendEvent;

public class RequestProxy implements CommandRuntimeStatusLI, CommandStatusLI {
    static Logger log = Logger.getLogger(RequestProxy.class);	
    XmlRpcClient client;
    String uri;

    public RequestProxy(String uri) throws java.net.MalformedURLException {
    	this.uri = uri;
    	log.debug("Creating XML-RPC client for proxy: " + uri);
    	client = new XmlRpcClientLite(uri);
    	log.debug("Proxy created.");
    }

    public void commandStarted(StartEvent event) { 
    	log.info("Send start event to " + uri);
    	try {
    		Vector params = new Vector();
    		params.add(Long.toString(event.getCommandID()));
    		params.add(event.getMessage());
    		params.add(event.getDate());
    		params.add(event.getMachine());

    		client.execute("Request.commandStarted", params);
    	} catch(IOException e) {
    		log.warn("In commandStarted io error with XML-RPC for " + uri + " " + e.getMessage(), e);
    	} catch(XmlRpcException e) {
    		log.warn("xmlrpcexception in requestproxy: " + e.getMessage(), e);
    	}
    	log.info(" Finished sending start event to " + uri);
    }

    public void commandRestarted(StartEvent event) {
    	log.info("Send restart event to " + uri);
		try {
		    Vector params = new Vector();
		    params.add(Long.toString(event.getCommandID()));
		    params.add(event.getMessage());
		    params.add(event.getDate());
		    params.add(event.getMachine());
		    client.execute("Request.commandStarted", params);
		} catch(IOException e) {
		    log.warn("IO error with XML-RPC for " + uri + " "  + e.getMessage(), e);
		} catch(XmlRpcException e) {
		    log.warn("XML-RPC exception in requestproxy  " + e.getMessage(), e);
		}
		log.info(" Finished restart event to " + uri);
    }


    public void commandFinished(FinishEvent event) { 
		log.info("Send finish event to " + uri);
		try {
		    Vector params = new Vector();
	
		    params.add(Long.toString(event.getCommandID()));
		    params.add(event.getMessage());
		    params.add(event.getDate());
		    params.add(new Integer(event.getReturnValue()));
	
		    client.execute("Request.commandFinished", params);
		} catch(IOException e) {
		    log.warn("commandFinished io error with XML-RPC for " + uri + " "  + e.getMessage());
		} catch(XmlRpcException e) {
		    log.warn("XML-RPC exception in requestproxy " + e.getMessage(), e);    
		}
		log.info("Finished sending finish event to " + uri);
    } 

    public void commandSubmitted(SubmitEvent pEvent) {
    	log.info("Send submit event to " + uri);
		try {
		    Vector params = new Vector();
		    params.add(Long.toString(pEvent.getCommandID()));
		    params.add(pEvent.getMessage());
		    params.add(pEvent.getDate());
		    params.add(pEvent.getMachine());
		    params.add(pEvent.getUser());
		    params.add(pEvent.getLogLocation());
	        if (log.isDebugEnabled()) {
	            log.debug(" params to submit event: " + params);
	        }
		    client.execute("Request.commandSubmitted", params);
		} catch(IOException e) {
		    log.warn("IO error with XML-RPC for " + uri + " "  + e.getMessage(), e);
		} catch(XmlRpcException e) {
		    log.warn("XmlRpcException in requestproxy  " + e.getMessage(), e);
		    
		}
		log.info("Finished sending submit event to " + uri);
    }

    public void commandInterrupted(InterruptEvent event) {
    	log.info("Send interrupt event to " + uri);
		try {
		    Vector params = new Vector();
		    params.add(Long.toString(event.getCommandID()));
		    params.add(event.getMessage());
		    params.add(event.getDate());
		    params.add(event.getInterrupt().toString());
		client.execute("Request.commandInterrupted", params);
		} catch(IOException e) {
		    log.warn("IO error with XML-RPC for " + uri + " "  + e.getMessage());
		} catch(XmlRpcException e) {
		    log.warn("XmlRpcException in requestproxy  " + e.getMessage());
		    
		}
		log.info("Finished sending interrupt event to " + uri);
    }

    public void commandResumed(ResumeEvent event) {
    	log.info("Send resume event to " + uri);
		try {
		    Vector params = new Vector();
		    params.add(Long.toString(event.getCommandID()));
		    
		    params.add(event.getMessage());
		    params.add(event.getDate());
		    params.add(event.getMachine());
		    client.execute("Request.commandResumed", params);
		} catch(IOException e) {
		    log.error("IO error with XML-RPC for " + uri + " "  + e.getMessage());
		} catch(XmlRpcException e) {
		    log.warn("XmlRpcException: " + e.getMessage(), e);
		}
		log.info("Finished sending resume event to " + uri);
    }

    public void commandSuspended(SuspendEvent event) {
    	log.info("Send suspend event to " + uri);
		try {
		    Vector params = new Vector();
		    params.add(Long.toString(event.getCommandID()));
		    
		    params.add(event.getMessage());
		    params.add(event.getDate());
		    client.execute("Request.commandSuspended", params);
		} catch(IOException e) {
		    log.error("IO error with XML-RPC for " + uri + " "  + e.getMessage());
		} catch(XmlRpcException e) {
		    log.warn("XmlRpcException: " + e.getMessage(), e);
		}
		log.info("Finished sending suspend event to " + uri);
    }

    public void commandFailed(FailureEvent event) { 
    	log.info("Send failed event to " + uri);
		try {
		    Vector params = new Vector();
		    params.add(Long.toString(event.getCommandID()));
		    params.add(event.getMessage());
		    params.add(event.getDate());
		    params.add(event.getFailure().getMessage());
		    client.execute("Request.commandFailed", params);
		} catch(IOException e) {
		    log.error("IO error with XML-RPC for " + uri + " "  + e.getMessage());
		} catch(XmlRpcException e) {
		    log.warn("XmlRpcException: " + e.getMessage(), e);
		    
		}
		log.info("Finished sending failed event to " + uri);
    }
}


